package org.ezplatform.office.rd.ldbb.service;

import org.ezplatform.core.dao.jpa.JpaBaseDao;
import org.ezplatform.core.pagination.PropertyFilter;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.office.rd.ldbb.entity.TaskprogressEntity;
import org.ezplatform.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gaoyw
 * @date 2024年08月08日 9:11
 */
@Service("ldbbService")
public class LdbbService extends BaseService<TaskprogressEntity, String> {
    public static final Logger LOG = LoggerFactory.getLogger(LdbbService.class);

    public Page<Map> getLdbbData(HttpServletRequest request, String leaderId,String wjbt) {
        Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
//        String wjbt = StringUtils.null2String(request.getParameter("wjbt"));
//        String formid = StringUtils.null2String(request.getParameter("formid"));
        List buildFilters = PropertyFilter.buildFiltersFromHttpRequest(TaskprogressEntity.class, request);
        StringBuilder sqlStr = new StringBuilder();
        Map paramMap = new HashMap();
//        paramMap.put("formid", formid);
        sqlStr.append(" SELECT  MAX(ID_) as ID_,  MAX(PROC_INST_ID_) as PROC_INST_ID_, MAX(BUSINESS_KEY_) as BUSINESS_KEY_, MAX(PROC_DEF_ID_) as PROC_DEF_ID_, \n" +
                " MAX(START_TIME_) as START_TIME_, MAX(END_TIME_) as END_TIME_,  MAX(START_USER_ID_) as START_USER_ID_,   MAX(EXT_STARTUSERID) as EXT_STARTUSERID,\n" +
                " MAX(EXT_STARTUSERNAME) as EXT_STARTUSERNAME, MAX(EXT_STARTUSERGROUPID) as EXT_STARTUSERGROUPID, MAX(EXT_STARTUSERGROUPNAME) as EXT_STARTUSERGROUPNAME,\n" +
                " MAX(EXT_TITLE) as EXT_TITLE, MAX(EXT_PROCESSSTATUS) as EXT_PROCESSSTATUS,  MAX(EXT_MODULE) as EXT_MODULE, MAX(EXT_CATEGORY) as EXT_CATEGORY\n" +
                " ,MAX(formKey) formKey, MAX(EXT_PREWARNINGDATE) as EXT_PREWARNINGDATE, max(EXT_WARNINGDATE) as EXT_WARNINGDATE , max(EXT_URGENTFIELD) as EXT_URGENTFIELD,\n" +
                " MAX(description) as extTaskUpdateTime,MAX(EXT_PROCESSCODE) as EXT_PROCESSCODE,\tCOUNT(taskId) AS taskCount,MAX(taskId) AS taskId,\n" +
                " MAX(taskEndTime) AS taskEndTime,Max(taskPreWarningDate) taskPreWarningDate,Max(taskWarningDate) taskWarningDate from \n" +
                " ( SELECT  RES.*,res2.ID_ as taskId,res2.end_time_ taskEndTime,res2.DESCRIPTION_ description,res2.ext_prewarningdate taskPreWarningDate,\n" +
                " res2.ext_warningdate taskWarningDate,res2.FORM_KEY_ formKey  FROM  ACT_HI_PROCINST RES \n" +
                " inner join act_hi_taskinst res2 on RES.id_=res2.proc_inst_id_  \n" +
                " right JOIN ldbbwjids ldbb ON ldbb.gwdataid=RES.business_key_ \n" +
                " WHERE  RES.EXT_MODULE =  'goa_document'  \n" +
                " AND ldbb.bbzt =  '1'   \n" +
//                " AND RES.EXT_CATEGORY='goa_doc_receivefile' \n" +
                " AND RES.EXT_PROCESSSTATUS in ('1','2','6')  \n" +
                " AND RES.TENANT_ID_ =  '0'   \n" +
                " AND res2.EXT_ISSTART='0' \n ");
        if (StringUtils.isNotEmpty(wjbt)) {
            sqlStr.append(" and RES.ext_title like :wjbt");
            paramMap.put("wjbt", "%" + wjbt + "%");
        }


        sqlStr.append(" AND (res2.EXT_HIDE_DONELIST = '0')  \n" +
                " AND res2.END_TIME_ IS NOT NULL \n" +
                " AND (res2.ASSIGNEE_ = '" + leaderId + "' or res2.EXT_DELEGATE_USERID = '" + leaderId + "' ) ORDER BY res2.end_time_ DESC ) t1  \n" +
                " GROUP BY PROC_INST_ID_ \n" +
                " ORDER BY taskEndTime DESC,START_TIME_ DESC ");

//        sqlStr.append(" order by createdDate desc");

        String sql = PropertyFilter.getSqlByFilterAndPageable("", sqlStr.toString(), "", buildFilters, pageable);
        return super.findByPageNativeSQL(pageable, sql, paramMap);
    }

    @Override
    protected JpaBaseDao<TaskprogressEntity, String> getEntityDao() {
        return null;
    }
}